using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PaperFighter;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using System.IO.IsolatedStorage;

namespace PaperFighter
{
    class Select : GameScreen
    {
        Texture2D background;
        Texture2D start;
        Texture2D blank;
        List<Texture2D> plane = new List<Texture2D>(3);
        List<Texture2D> planeInfo = new List<Texture2D>(3);
        List<Rectangle> planeRectangle = new List<Rectangle>(3);
        List<Vector2> planePosition = new List<Vector2>(3);
        List<Vector2> planeInfoPosition = new List<Vector2>(3);
        SoundEffect clickSound;

        int delay = 2;
        int delayCount = 0;
        int frameTotal = 9;
        int frameIndex = 0;
        int frameDirection = 1;
        int frameWidth;
        int frameHeight;
        int planeSelected = 0;
        int planeHold = -1;
        float enterSpeed = 0;
        float transparence = 0;
        float exitTransparence = 0;
        bool startClick = false;

        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;


        public Select()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.0);
            TransitionOffTime = TimeSpan.FromSeconds(0.0);
        }

        public override void LoadContent()
        {
            background = ScreenManager.Game.Content.Load<Texture2D>("background_1");
            start = ScreenManager.Game.Content.Load<Texture2D>("start_1");
            blank = ScreenManager.Game.Content.Load<Texture2D>("blank");
            clickSound = ScreenManager.Game.Content.Load<SoundEffect>("click_1");
            plane.Add(ScreenManager.Game.Content.Load<Texture2D>("plane_2_1"));
            plane.Add(ScreenManager.Game.Content.Load<Texture2D>("plane_2_2"));
            plane.Add(ScreenManager.Game.Content.Load<Texture2D>("plane_2_3"));
            planeInfo.Add(ScreenManager.Game.Content.Load<Texture2D>("info_1"));
            planeInfo.Add(ScreenManager.Game.Content.Load<Texture2D>("info_2"));
            planeInfo.Add(ScreenManager.Game.Content.Load<Texture2D>("info_3"));
            planeRectangle.Add(new Rectangle());
            planeRectangle.Add(new Rectangle());
            planeRectangle.Add(new Rectangle());
            planePosition.Add(new Vector2(-150, 40));
            planePosition.Add(new Vector2(-150, 240));
            planePosition.Add(new Vector2(-150, 440));
            planeInfoPosition.Add(new Vector2(230, 59));
            planeInfoPosition.Add(new Vector2(230, 259));
            planeInfoPosition.Add(new Vector2(230, 459));

            frameWidth = plane[0].Width / (frameTotal + 2);
            frameHeight = plane[0].Height;

            base.LoadContent();
        }

        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            PlayerIndex playerIndex;
            if (input.IsMenuCancel(null, out playerIndex))
                ScreenManager.Game.Exit();

            planeHold = -1;
            if (input.TouchState.Count > 0)
            {
                int touchPositionY = (int)input.TouchState[0].Position.Y;

                if (touchPositionY >= 50 && touchPositionY <= 200)
                    planeSelected = planeHold = 0;

                if (touchPositionY >= 250 && touchPositionY <= 400)
                    planeSelected = planeHold = 1;

                if (touchPositionY >= 450 && touchPositionY <= 600)
                    planeSelected = planeHold = 2;

                if (touchPositionY >= 650)
                    startClick = startClick == false ? clickSound.Play() : true;
            }

            base.HandleInput(input);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            planeRectangle[0] = new Rectangle(frameTotal * frameWidth, 0, frameWidth, frameHeight);
            planeRectangle[1] = new Rectangle(frameTotal * frameWidth, 0, frameWidth, frameHeight);
            planeRectangle[2] = new Rectangle(frameTotal * frameWidth, 0, frameWidth, frameHeight);
            planeRectangle[planeSelected] = new Rectangle(frameIndex * frameWidth, 0, frameWidth, frameHeight);

            if (planeHold >= 0)
                planeRectangle[planeHold] = new Rectangle((frameTotal + 1) * frameWidth, 0, frameWidth, frameHeight);

            if (delayCount++ >= delay)
            {
                delayCount = 0;
                frameIndex += frameDirection;
                if (frameIndex >= frameTotal || frameIndex < 0)
                {
                    frameDirection = -frameDirection;
                    frameIndex += frameDirection;
                }
            }

            if (50 - planePosition[0].X > 0)
            {
                enterSpeed = (25 - planePosition[0].X) / 10 + 5;
                enterSpeed = enterSpeed > 25 - planePosition[0].X ? 25 - planePosition[0].X : enterSpeed;
                planePosition[0] = planePosition[0] + new Vector2(enterSpeed, 0);
                planePosition[1] = planePosition[1] + new Vector2(enterSpeed, 0);
                planePosition[2] = planePosition[2] + new Vector2(enterSpeed, 0);
            }

            transparence = transparence < 1 ? (transparence + 0.02f) * 1.1f : 1;

            if (startClick)
            {
                exitTransparence += 0.05f;
                if (exitTransparence > 1)
                {
                    ExitScreen();

                    string showTutorial;
                    if (settings.TryGetValue<String>("showTutorial", out showTutorial))
                    {
                        if (showTutorial == "false")
                            ScreenManager.AddScreen(new Play1(planeSelected), null);
                        else
                            ScreenManager.AddScreen(new Tutorial(planeSelected), null);
                    }
                    else
                        ScreenManager.AddScreen(new Tutorial(planeSelected), null);
                }
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.Draw(background, Vector2.Zero, Color.White);
            ScreenManager.SpriteBatch.Draw(start, new Vector2(240 - start.Width / 2, 650), Color.White * transparence);

            for (int i = 0; i < 3; i++)
            {
                ScreenManager.SpriteBatch.Draw(plane[i], planePosition[i], planeRectangle[i], Color.White);
                ScreenManager.SpriteBatch.Draw(planeInfo[i], planeInfoPosition[i], Color.White * transparence);
            }

            if (startClick)
                ScreenManager.SpriteBatch.Draw(blank, Vector2.Zero, new Rectangle(0, 0, 480, 800), Color.White * exitTransparence);

            ScreenManager.SpriteBatch.End();
        }
    }
}
